#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.8.1
 Author:         myName

 Script Function:
	Template AutoIt script.

#ce ----------------------------------------------------------------------------


Global $monster_ready[6] = [90, 150, 0x653034, 70, 60, 0x000000]
Global $monster_box[4] = [0, 310, 169, 479]
Global $monster_point[5] = [69, 70, 77, 84, 90]

Global $monster_hBMP
Global $monster_tBits
Global $monster_width = $monster_box[2] - $monster_box[0] + 1
Global $monster_height = $monster_box[3] - $monster_box[1] + 1
Global $monster_length = $monster_width * $monster_height

Global $monster_loot_box[4] = [91, 121, 640, 460]

Global $monster_loot_hBMP
Global $monster_loot_tBits
Global $monster_loot_width = $monster_loot_box[2] - $monster_loot_box[0] + 1
Global $monster_loot_height = $monster_loot_box[3] - $monster_loot_box[1] + 1
Global $monster_loot_length = $monster_loot_width * $monster_loot_height

Global $monster_loot_close[7] = [ _
	$monster_loot_width - 30, 0, _
	$monster_loot_width - 1, 30, _
	0x220000,-1,-1]

Global $monster_loot_search[4] = [413,220,268,200]
Global $monster_loot_place[3][2]
Global $monster_loot_shift[2] = [-29,-64]
Global $monster_loot_count = 0

Global $monster_loot_trash[10][5] = [ _
	["hranitel vremen", 0x3C2221, 0xFFEFE2, 0x7A7960, 0x9F984B], _
	["sleza gad", 0x631A32, 0x290613, 0x660219, 0x991738], _
	["kirasa proshlogo", 0xC1B09A, 0x540100, 0x6A5F47, 0x87745F], _
	["random", 0x614220, 0x5B3C1B, 0x5D3D1D, 0x553217], _
	["ceremon dospeh", 0xDD9B3A, 0x8E6020, 0x594D1C, 0x2A2314], _
	["sword", 0x57605E, 0x32444D, 0xC1D0DC, 0xB1B9BE], _
	["dospeh sili", 0x2D140B, 0x5D3E03, 0x8D8472, 0x5E5541], _
	["kilco", 0x574512, 0xB07E36, 0x503B00, 0x411C00], _
	["rubaha", 0x3C4425, 0x402D26, 0xC68B46, 0x383428], _
	["relikv", 0xDFDFDF, 0x9DA3B0, 0xC3A62E, 0xB2B2B2] _
]

Global $monster_dict[200]
Global $monster_dict_cnt = 0
Global $monster_times = 40
Global $monster_delay = 4

Global $monster_dict_in = "monster-in.txt"
Global $monster_dict_out = "monster-out.txt"
Global $monster_dict_path = "monster"

Func monster_loot_capture($pos)
	If $monster_loot_hBMP Then
		_WinAPI_DeleteObject($monster_loot_hBMP)
		$monster_loot_hBMP = 0
	EndIf
	If $monster_loot_tBits Then
		_WinAPI_DeleteObject($monster_loot_tBits)
		$monster_loot_tBits = 0
	EndIf
	$monster_loot_tBits = DllStructCreate('dword[' & $monster_loot_length & ']')
	Local $p = $winlist[$pos]
	Local $l = $monster_loot_box[0] + $p[0] + $p[2]
	Local $r = $monster_loot_box[2] + $p[0] + $p[2]
	Local $t = $monster_loot_box[1] + $p[1] + $p[3]
	Local $b = $monster_loot_box[3] + $p[1] + $p[3]
	;_DebugOut("capture: " & $l & "," & $t & "," & $r & "," & $b)
	
	$monster_loot_hBMP = _ScreenCapture_Capture("",$l,$t,$r,$b)
	_WinAPI_GetBitmapBits($monster_loot_hBMP, 4 * $monster_loot_length, DllStructGetPtr($monster_loot_tBits))
EndFunc

Func monster_capture($pos)
	If $monster_hBMP Then
		_WinAPI_DeleteObject($monster_hBMP)
		$monster_hBMP = 0
	EndIf
	If $monster_tBits Then
		_WinAPI_DeleteObject($monster_tBits)
		$monster_tBits = 0
	EndIf
	$monster_tBits = DllStructCreate('dword[' & $monster_length & ']')
	Local $p = $winlist[$pos]
	Local $l = $monster_box[0] + $p[0] + $p[2]
	Local $r = $monster_box[2] + $p[0] + $p[2]
	Local $t = $monster_box[1] + $p[1] + $p[3]
	Local $b = $monster_box[3] + $p[1] + $p[3]
	;_DebugOut("capture: " & $l & "," & $t & "," & $r & "," & $b)
	
	$monster_hBMP = _ScreenCapture_Capture("",$l,$t,$r,$b)
	_WinAPI_GetBitmapBits($monster_hBMP, 4 * $monster_length, DllStructGetPtr($monster_tBits))
EndFunc

Func monster_ident($pos,$times)
	Local $point = UBound($monster_point) - 2
	Local $mark[$times][$point + 1]
	For $t = 0 To $times - 1
		Sleep($monster_delay)
		monster_capture($pos)
		For $p = 0 To $point
			$mark[$t][$p] = getPixel($monster_tBits,$monster_width,$monster_point[0],$monster_point[$p+1])
		Next
	Next
	Return $mark
EndFunc

Global $monster_need_load = True

Func monster_reg(ByRef $mark,$create = True)
	If $monster_need_load Then
		monster_dict_load()
		monster_dict_save()
		$monster_need_load = False
	EndIf
	Local $point = UBound($monster_point) - 2
	Local $found = -1
	Local $msize = UBound($mark) - 1
	For $i = 0 To $monster_dict_cnt - 1
		Local $dict = $monster_dict[$i]
		Local $dsize = UBound($dict,1) - 1
		For $d = 0 To $dsize
			For $m = 0 To $msize
				Local $cnt = 0
				For $p = 0 To $point
					If $mark[$m][$p] = $dict[$d][$p] Then
					$cnt += 1
					EndIf
				Next
				If $point - $cnt < 2 Then
					_DebugOut("found d=" & $d & " m=" & $m & " cnt=" & $cnt)
					$found = $i
					ExitLoop 3
				EndIf
			Next
		Next
	Next
	If $create And $found < 0 Then
		$found	= $monster_dict_cnt
		$monster_dict[$monster_dict_cnt] = $mark
		$monster_dict_cnt += 1
		SetExtended(1)
	EndIf
	Return $found
EndFunc

Func monster_recognize($pos)
	Local $m = monster_ident($pos,1)
	Local $id = monster_reg($m,False)
	If $id < 0 Then
		$m = monster_ident($pos,$monster_times)
		$id = monster_reg($m)
		Local $e = @extended
		SetExtended($e)
	EndIf
	Return $id
EndFunc

Func monster_ready($pos)
	Local $id = monster_recognize($pos)
	Local $new = @extended
	_DebugOut("found monster: " & $id)
	If $new Then
		_DebugOut("new monster: " & $id)
		Local $file = $monster_dict_path & "\monster-" & $id  & ".jpg"
		If FileExists($file) Then
			Local $file_back = $monster_dict_path & "\monster-" & $id  & "-" & Random(10000,99999,1) & ".jpg"
			_DebugOut("overwrite file: " & $file & ", old moved to " & $file_back)
			FileMove($file,$file_back,1)
		EndIf
		_ScreenCapture_SaveImage($file,$monster_hBMP, False)
		_DebugOut("store new monster: " & $file)
		monster_dict_save()
	EndIf
	;Return -4
	monster_show($id,$new)
	Return $id
EndFunc

Func monster_open($pos)
	Local $x = $monster_box[0] + $monster_point[0]
	For $iy = 1 To UBound($monster_point) - 1
		Local $y = $monster_box[1] + $monster_point[$iy]
		;_DebugOut("open loot: " & $x & ", " & $y)
		make_click($pos,$x, $y)
	Next
EndFunc

Global $loot_cnt = 0

Func monster_close($pos)
	make_click($pos,$monster_loot_box[0] + $monster_loot_close[5], $monster_loot_box[1] + $monster_loot_close[6])
EndFunc

Func monster_loot_ready($pos)
	monster_loot_capture($pos)
	If monster_loot_no_close($pos) Then
		_DebugOut("no close")
		Return False
	EndIf
	Local $l = monster_loot_search($pos)
	If $l[0] < 0 Then
		_DebugOut("no sy")
		Return False
	ElseIf $l[1] < $monster_loot_search[3] Then
		If $l[2] < $monster_loot_search[3] Then
			$monster_loot_place[0][0] = $monster_loot_search[0] + $monster_loot_shift[0] - 60
			$monster_loot_place[0][1] = $l[0] + $monster_loot_shift[1]
			$monster_loot_place[1][0] = $monster_loot_search[0] + $monster_loot_shift[0]
			$monster_loot_place[1][1] = $l[0] + $monster_loot_shift[1]
			$monster_loot_place[2][0] = $monster_loot_search[0] + $monster_loot_shift[0] + 60
			$monster_loot_place[2][1] = $l[0] + $monster_loot_shift[1]
			$monster_loot_count = 3
		Else
			$monster_loot_place[0][0] = $monster_loot_search[0] + $monster_loot_shift[0]
			$monster_loot_place[0][1] = $l[0] + $monster_loot_shift[1]
			$monster_loot_count = 1
		EndIf
	ElseIf $l[3] < $monster_loot_search[3] Then
		$monster_loot_place[0][0] = $monster_loot_search[0] + $monster_loot_shift[0] - 30
		$monster_loot_place[0][1] = $l[0] + $monster_loot_shift[1]
		$monster_loot_place[1][0] = $monster_loot_search[0] + $monster_loot_shift[0] - 30 + 60
		$monster_loot_place[1][1] = $l[0] + $monster_loot_shift[1]
		$monster_loot_count = 2
	Else
		Return False
	EndIf
	Return True
EndFunc

Func monster_loot_no_close($pos)
	For $x = $monster_loot_close[0] To $monster_loot_close[2]
		For $y = $monster_loot_close[1] To $monster_loot_close[3]
			Local $color = getPixel($monster_loot_tBits,$monster_loot_width,$x,$y)
			If $color = $monster_loot_close[4] Then
				;$loot_cnt += 1
				;Local $file = "loot\0-loot-capture-"  & $loot_cnt & ".bmp"
				;_ScreenCapture_SaveImage($file,$monster_loot_hBMP, False)
				;_DebugOut("save: " & $file)
				$monster_loot_close[5] = $x
				$monster_loot_close[6] = $y
				Return False
			EndIf
		Next
	Next
	Return True
EndFunc

Func monster_loot_search($pos)
	Local $sy = -1
	For $y = $monster_loot_search[2] To $monster_loot_search[1] Step -1
		Local $cur = getPixelLight($monster_loot_tBits,$monster_loot_width,$monster_loot_search[0],$y)
		;_DebugOut("light: p= " & $y & ", " & $monster_loot_search[0] & " l= " & $cur)
		If $cur < $monster_loot_search[3] Then
			$sy = $y
			ExitLoop
		EndIf
	Next
	If $sy < 0 Then
		_DebugOut("no line for sy")
		Local $r[1] = [-1]
		Return $r
	EndIf
	Local $light[4] = [$sy, _
		getPixelLight($monster_loot_tBits,$monster_loot_width,$monster_loot_search[0],$sy - 9), _
		getPixelLight($monster_loot_tBits,$monster_loot_width,$monster_loot_search[0] + 60,$sy - 9), _
		getPixelLight($monster_loot_tBits,$monster_loot_width,$monster_loot_search[0] + 30,$sy - 9) _
	]
	Return $light
EndFunc

Func monster_loot_trash(ByRef $colors)
	For $j = 0 To UBound($monster_loot_trash,1) - 1
		Local $found = True
		For $i = 0 To UBound($colors) - 1
			If $monster_loot_trash[$j][$i+1] <> $colors[$i] Then
				$found = False
				ExitLoop
			EndIf
		Next
		If $found Then
			Return $monster_loot_trash[$j][0]
		EndIf
	Next
	Return ""
EndFunc

Func monster_loot_has($pos,$loot)
	Local $p = $winlist[$pos]
	Local $found = False
	For $i = 0 To $monster_loot_count - 1
		Local $colors[UBound($shop_item,1)]
		For $j = 0 To UBound($shop_item,1) - 1
			Local $x = $monster_loot_place[$i][0] + $shop_item[$j][0]
			Local $y = $monster_loot_place[$i][1] + $shop_item[$j][1]
			$colors[$j] = getPixel($monster_loot_tBits,$monster_loot_width,$x,$y)
		Next
		Local $item = detect_shop_item($colors,False)
		If $item < 0 Then
			$item = monster_loot_trash($colors)
			If $item == ""  Then
				$item = "no founed item ["
				For $j = 0 To UBound($shop_item,1) - 1
					$item &= "0x" & Hex($colors[$j],6) & ", "
				Next
				$item &= "] on " & $loot_cnt & " place " & $i
	
				If True Then
					Local $l = $monster_loot_place[$i][0] + $p[0] + $p[2] + $monster_loot_box[0]
					Local $t = $monster_loot_place[$i][1] + $p[1] + $p[3] + $monster_loot_box[1]
					Local $r = $l + 59
					Local $b = $t + 59
					Local $file = "loot\place-" & $loot_cnt & "-" & $i & ".bmp"
					_ScreenCapture_Capture($file,$l,$t,$r,$b)
					$loot_cnt += 1
				EndIf
				$found = True
			EndIf
		EndIf
		_DebugOut("found loot: " & $item)
	Next
	_DebugOut("Loot: " & " count=" & $monster_loot_count)
	Return $found
EndFunc

Func monster_dict_save()
	Local $fh = FileOpen($monster_dict_out,BitOR(2,256))
	Local $point = UBound($monster_point) - 2
	For $i = 0 To $monster_dict_cnt - 1
		Local $dict = $monster_dict[$i]
		Local $line = $i
		Local $times = UBound($dict,1)
		For $t = 0 To $times - 1
			Local $new = True
			For $k = 0 To $t - 1
				Local $dup = True
				For $p = 0 To $point
					If $dict[$t][$p] <> $dict[$k][$p] Then
						$dup = False
						ExitLoop
					EndIf
				Next
				If $dup Then
					$new = False
					ExitLoop
				EndIf
			Next
			If $new Then
				For $p = 0 To $point
					$line &= " 0x" & Hex($dict[$t][$p],6)
				Next
			EndIf
		Next
		FileWrite($fh, $line & @CRLF)
	Next
	FileClose($fh)
EndFunc

Func monster_dict_load()
	Local $fh = FileOpen($monster_dict_in)
	$monster_dict_cnt = 0
	If $fh = -1 Then
		Return
	EndIf
	
	Local $point = UBound($monster_point) - 2
	Local $re = "(\d+)"

	Local $re2 = "("
	Local $re3 = "(\w+)"
	For $p = 0 To $point
		$re2 &= "\s+\w+"
	Next
	$re2 &= ")?"

	For $t = 0 To $monster_times - 1
		$re &= $re2
	Next
	While True
		Local $line = FileReadLine($fh)
		If @error = -1 Then ExitLoop
		Local $ret = StringRegExp($line,$re,1)
		If @error = 0 Then
			Local $times = UBound($ret) - 1
			Local $dict[$times][$point + 1]
			For $t = 0 To $times - 1
				Local $row = StringRegExp($ret[$t + 1],$re3,3)
				If UBound($row) = $point + 1 Then
					For $p = 0 To $point
						$dict[$t][$p] = $row[$p]
					Next
				Else
					_DebugOut("bad line i=" & $ret[0] & " grp=" & $times & " t=" & $t)
				EndIf
			Next
			;_DebugOut("row " & $ret[0] & " has " & $times)
			$monster_dict[$ret[0]] = $dict
			If $monster_dict_cnt < $ret[0] + 1 Then
				$monster_dict_cnt = $ret[0] + 1
			EndIf
		Else
			_DebugOut("Bad string: " & $line)
		EndIf
	WEnd
	FileClose($fh)
EndFunc

Global $monster_show
Global $monster_show_g
Global $monster_show_l
Func monster_show($id,$new)
	If Not $monster_show Then
		Local $pos = WinGetPos($hGUI)
		$monster_show = GUICreate("monster show",$monster_width,$monster_height + 20,$pos[0]+20,$pos[1]+20,BitOr($WS_CAPTION, $WS_POPUP))
		GUISwitch($monster_show)
		$monster_show_g = GUICtrlCreatePic("",0,0,$monster_width,$monster_height)
		$monster_show_l = GUICtrlCreateLabel("",0,$monster_height,$monster_width,20)
		GUISetState(@SW_SHOW,$monster_show)
		GUISwitch($hGUI)
	EndIf
	Local $file = $monster_dict_path & "\monster-" & $id  & ".jpg"
	GUICtrlSetImage($monster_show_g, $file)
	Local $str = "(    " & $id & "    )"
	If $new Then
		$str &= "    *** new ***    "
	EndIf
	GUICtrlSetData($monster_show_l, $str)
EndFunc
